import { RootState } from "@/store"
import { getSubjectTree } from "@/utils/api/subjectApi"
import { AxiosRes } from "@/utils/http"
import { createAsyncThunk, createSlice } from "@reduxjs/toolkit"


const initialState={
    subject_tree:[],//树结构
    active_two:{
        title:'',
        value:''
    },//二级分类值
}

export const  get_subject_tree=createAsyncThunk(
    'get/suject_tree',
    async(action,state)=>{
        const res=await getSubjectTree()
        console.log(res);
        return res.data.data
    }
)


export const subjectSlice=createSlice({
    name:'subject',
    initialState,
    reducers:{
        set_subject_two:(state,active)=>{
             state.active_two=active.payload
        }
    },
    extraReducers:(builder)=>{
        builder.addCase(get_subject_tree.fulfilled,(state,res)=>{
            state.subject_tree=res.payload
        })
    }
})
export const select_tree_data=(state:RootState)=>{
    return state.subject.subject_tree
}
export const get_active_two=(state:RootState)=>{
    return state.subject.active_two
}

export const {set_subject_two} =subjectSlice.actions

export default subjectSlice.reducer